$(function () {
    $('#table-role').datagrid({
        url: ctxPath+"role",
        method:"get",
        fit:true,
        border:false,
        pagination:true,
        striped:true,
        rownumbers:true,
        pageNumber:1,
        pageSize:10,
        columns:[[
            // ,checkbox:true
            {field:'id',title:'角色id',width:100,align:"center"},
            {field:'roleName',title:'角色名称',width:100,align:'center'},
            {field:'fcd',title:'创建时间',width:100,align:'center'},
            {field:'lcd',title:'更新时间',width:100,align:'center'}
        ]],
        loadFilter: function(data){
            if (data.date){
                return data.date;
            } else {
                return data;
            }
        },
        toolbar:"#div_table_bar"
    });

    //新增按钮
    $("#btn_add").click(function () {
        $("#div_role").dialog("setTitle","修改角色");
        //打开新增窗口
        $('#div_role').dialog("open");
    });

    $("#btn_close").click(function () {
    //关闭新增窗口
        $('#div_role').dialog("close");
        //     清空对话框表单
        $("#form_role").form("clear");
    });


    // 提交按钮
    $("#btn_submit").click(function () {
        var method = "post";
        if($("#id").val().length>0){
            $("#_method").val("PUT");
        }else{
            $("#_method").val("");
        }
        //发起新增请求
        $('#form_role').form('submit', {
            url: ctxPath+"role",
            // method:"post",
            onSubmit:function(){
              return $("#form_role").form("validate");
            },
            success: function(response){
                response = $.parseJSON(response);
                if(response.code==20000){
                //    成功消息提示
                    $.messager.show({
                        title:'操作成功',
                        msg:'成功新增角色数据····',
                        timeout:5000,
                        showType:'slide'
                    });
                //     关闭对话框
                    $('#div_role').dialog("close");
                //     刷新表格
                    $('#table-role').datagrid('reload');
                    //     清空对话框表单
                    $("#form_role").form("clear");
                }else{
                    //错误提示
                    $.messager.alert('操作失败','角色新增失败请联系管理员！！！','error');
                }
            }
        });
    });
    //角色信息对话框
    $('#div_role').dialog({
        title: '新增角色',
        width: 400,
        height: 200,
        closed: true,
        cache: false,
        // href: 'get_content.php',
        modal: true,
        buttons: "#div_button",
        onClose: function () {
            //     清空对话框表单
            $("#form_role").form("clear");
        }
    });

    //角色名称
    $('#roleName').validatebox({
        required: true
    });

    //  权限下拉
    $('#auths').combotree({
        url: ctxPath+"menu",
        method:"get",
        required: true,
        multiple:true,
        checkbox:true,
        cascadeCheck:false,
        onlyLeafCheck:true,
        method:"get",
        loadFilter: function(data){
            return data.date;
        },
        onLoadSuccess:function(node,data){
            var _this = this;
            if(data){
                $(data).each(function(){
                    if(this.state == 'closed'){
                        $(_this).tree("expandAll");
                    }
                });
            }
        }

    });


    //修改按钮
    $("#btn_edit").click(function () {
        var rows = $('#table-role').datagrid("getChecked");
        if(rows.length == 0){
            $.messager.alert('提示','请选择一条记录',"warning");
        }else if (rows.length > 1) {
            $.messager.alert('提示','只能选择一条记录',"warning");
        }else {
            //初始化表单数据
            $('#form_role').form('load',ctxPath+'role/'+rows[0].id);
            //打开新增窗口
            // alert(rows[0].id);
            $("#div_role").dialog("setTitle","修改角色");
            $('#div_role').dialog("open");

        }
    });


    $("#btn_remove").click(function () {
        //至少选择一条
        var rows = $('#table-role').datagrid("getChecked");
        if(rows.length == 0){
            $.messager.alert('提示','请至少选择一条记录',"warning");
        }else{

            //确认删除操作
            $.messager.confirm('删除确认', '您确认删除选中记录？', function(r){
                var ids=new Array();
                for(i in rows){
                    ids.push(rows[i].id);
                }
                if (r){
                    var url = ctxPath+"role";
                    var params = {
                      _method:"DELETE",
                      ids:ids.join(",")
                    };
                    //删除：   异步DELETE请求
                    $.post(url,params,function (response) {
                        alert(response);
                        if(response.code=="20000") {
                            $.messager.show({
                                title: "删除成功",
                                msg: response.message,
                                timeout: 5000,
                                showType: "slide"
                            });
                            //删除成功刷新表格
                            $("#table-role").datagrid("reload");
                        }else {
                            //错误提示
                            $.messager.alert('操作失败','角色新增失败请联系管理员！！！','error');
                        }
                    });

                }
            });
        }
    });

});